package com.dwxt.cdms.constant;

import com.dwxt.cdms.api.common.utils.DateUtil;
import com.dwxt.cdms.entity.TaskMessage;
import com.dwxt.cdms.util.RedisUtil;
import com.dwxt.common.util.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
/**
 * @Description:消息管理
 * @author chendq
 * @date 2021/10/18
 */
public class TaskMessageData {

    //消息类型
    public static final String  TYPE_TIMING= "0";
    public static final String  TYPE_LOOP= "1";
    public static final String  TYPE_REGISTER= "2";
    public static final String  TYPE_BIRTHDAY= "3";

    //时间类型
    public static final String  DAY= "0";
    public static final String  WEEK= "1";
    public static final String  MONTH= "2";

    /**
     * @Description: 发送时间为每天循环时获取下一次发送的时间差
     * @param timeStr:
     * @return java.lang.Long
     * @author chendq
     * @date 2021/10/18
     */
    public static Long getDayTime(String timeStr) {
        Date now = new Date();
        String s = DateUtils.formatDate(now);
        String hms = timeStr.substring(timeStr.lastIndexOf(" ") + 1);
        String dateStr = s+" "+hms;
        Date date = DateUtils.formatTime(dateStr);
        if (date.getTime() - now.getTime() < 0) {//今天已过
            String tomorrow = DateUtils.getFetureDate(date, 1)+" "+hms;
            return (DateUtils.formatTime(tomorrow).getTime() - now.getTime())/1000;
        }
        //今天还没到时间
        return (date.getTime()-now.getTime())/1000;
    }

    /**
     * @Description: 发送时间为每周循环时获取下一次发送的时间差
     * @param timeStr:
     * @return java.lang.Long
     * @author chendq
     * @date 2021/10/18
     */
    public static Long getWeekTime(String timeStr) {
        try {
            Date nowDate = new Date();
            int weekDay = getWeekDay(timeStr.substring(timeStr.indexOf(" ") + 1, timeStr.lastIndexOf(" ")));
            String hms = timeStr.substring(timeStr.lastIndexOf(" "));
            Calendar cal = Calendar.getInstance();
            cal.setTime(nowDate);
            int nowWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (nowWeek > weekDay) {//已过
                String ymd = getNextWeek(weekDay);
                long time = DateUtil.parseToDate(ymd + " " + hms, "yyyy-MM-dd HH:mm:ss").getTime();
                return (time - nowDate.getTime()) / 1000;
            } else if (nowWeek < weekDay) {//还没到
                String ymd = getNowWeek(weekDay);
                long time = DateUtil.parseToDate(ymd + " " + hms, "yyyy-MM-dd HH:mm:ss").getTime();
                return (time - nowDate.getTime()) / 1000;
            } else {//当天
                String todayStr = DateUtils.formatDate(nowDate) + " " + hms;
                Date date = DateUtil.parseToDate(todayStr, "yyyy-MM-dd HH:mm:ss");
                long l = date.getTime() - nowDate.getTime();
                if (l > 0) {//今天还没到
                    return l / 1000;
                } else {//今天已经过了,那就下周的今天
                    long nextWeekDayTime = date.getTime() + (7 * 86400000L);
                    return (nextWeekDayTime - nowDate.getTime()) / 1000;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Description: 发送时间为每月循环时获取下一次发送的时间差
     * @param timeStr:
     * @return java.lang.Long
     * @author chendq
     * @date 2021/10/18
     */
    public static Long getMonthTime(String timeStr) {
        try {
            String[] arr = timeStr.split(" ");
            String dayStr = arr[1].split("日")[0];
            String hmsStr = arr[2];
            Date nowDate = new Date();
            String dateStr = DateUtils.format(nowDate, DateUtils.YYYY_MM) + "-" + dayStr + " " + hmsStr;
            Date date = DateUtil.parseToDate(dateStr, "yyyy-MM-dd HH:mm:ss");
            boolean b = date.getTime() > nowDate.getTime();
            if (date.getTime() > nowDate.getTime()) {//时间还没到
                return (date.getTime() - nowDate.getTime()) / 1000;
            } else {//已经过了，时间应为下个月
                Date nextMonthDate = TaskMessageData.getNextMonthDate(date, 1);
                return (nextMonthDate.getTime() - nowDate.getTime()) / 1000;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Description: 星期几的字符串转天数
     * @param weekStr:
     * @return int
     * @author chendq
     * @date 2021/10/18
     */
    public static int getWeekDay(String weekStr) {
        switch (weekStr) {
            case "星期日":
                return 0;
            case "星期一" :
                return 1;
            case "星期二":
                return 2;
            case "星期三" :
                return 3;
            case "星期四":
                return 4;
            case "星期五" :
                return 5;
            case "星期六":
                return 6;
        }
        return 0;
    }


    /**
     * @Description: 获取下一周
     * @param weekNum:
     * @return java.lang.String
     * @author chendq
     * @date 2021/10/18
     */
    public static String getNextWeek(int weekNum){
        String nextWeekData=null;
        Long nowDateNum;
        Long dayNum=86400000L;

        String nowWeek=new SimpleDateFormat("EEEE").format(new Date());
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            Long dateNum;
            date=simpleDateFormat.parse(simpleDateFormat.format(new Date()));
            nowDateNum=date.getTime();
            if (nowWeek.equals("星期一")){
                dateNum=nowDateNum+dayNum*(6+weekNum);
            }else if(nowWeek.equals("星期二")){
                dateNum=nowDateNum+dayNum*(5+weekNum);
            } else if(nowWeek.equals("星期三")){
                dateNum=nowDateNum+dayNum*(4+weekNum);
            }else if(nowWeek.equals("星期四")){
                dateNum=nowDateNum+dayNum*(3+weekNum);
            }else if(nowWeek.equals("星期五")){
                dateNum=nowDateNum+dayNum*(2+weekNum);
            }else if(nowWeek.equals("星期六")){
                dateNum=nowDateNum+dayNum*(1+weekNum);
            }else{
                dateNum=nowDateNum+dayNum*weekNum;
            }
            nextWeekData=simpleDateFormat.format(new Date(dateNum));
        }catch (ParseException e){
            e.printStackTrace();
        }
        return nextWeekData;
    }

    /**
     * @Description: 获取本周
     * @param weekNum:
     * @return java.lang.String
     * @author chendq
     * @date 2021/10/18
     */
    public static String getNowWeek(int weekNum){
        String nextWeekData=null;
        Long nowDateNum;
        Long dayNum=86400000L;

        String nowWeek=new SimpleDateFormat("EEEE").format(new Date());
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            Long dateNum;
            date=simpleDateFormat.parse(simpleDateFormat.format(new Date()));
            nowDateNum=date.getTime();
            dateNum=nowDateNum+dayNum;
            nextWeekData=simpleDateFormat.format(new Date(dateNum));
        }catch (ParseException e){
            e.printStackTrace();
        }
        return nextWeekData;
    }

    /**
     * @Description: 获取下一个月几号
     * @param startDate:
     * @param month:
     * @return java.util.Date
     * @author chendq
     * @date 2021/10/18
     */
    public static Date getNextMonthDate(Date startDate,int month){
        LocalDateTime localDateTime = startDate.toInstant()
                .atZone(ZoneId.systemDefault() )
                .toLocalDateTime().plusMonths(month);
        Date date = Date.from(localDateTime.atZone( ZoneId.systemDefault()).toInstant());
        return date;
    }

    //分
    private static Integer  branch=60;
    //时
    private static Integer  time=60*60;
    //天
    private static Integer  days= 60*60*24;

    public static String conversion(long timeDifference) {
        //转换
        String timedesc=null;
        long minutes;
        long remainingSeconds;
        long hour;
        long day;
        if (timeDifference<branch){
            timedesc=(timeDifference)+"秒";
        }else  if (timeDifference>branch-1&&timeDifference<time){
            minutes = timeDifference / branch;
            remainingSeconds= timeDifference % branch;
            timedesc= minutes +"分"+remainingSeconds+"秒";
        }else if (timeDifference>time-1&&timeDifference<days){
            hour=timeDifference/time;
            remainingSeconds = (timeDifference%time)/branch;
            minutes = (timeDifference%days)%branch;
            timedesc=hour+"时"+minutes +"分"+remainingSeconds+"秒";
        }else if(timeDifference>days-1){
            day= timeDifference/ ( days);
            hour = (timeDifference% ( days)) / (time);
            minutes = (timeDifference % (time)) /branch;
            remainingSeconds = timeDifference % branch;
            timedesc=day+"天"+hour+"时"+minutes +"分"+remainingSeconds+"秒";
        }
        return timedesc;
    }

    /**
     * @Description: 设置redis失效时间，失效就发送消息
     * @param taskMessage:
     * @return void
     * @author chendq
     * @date 2021/10/18
     */
    public static void setRedisValue(TaskMessage taskMessage) {
        try {
            String timeStr = taskMessage.getTime();
            if (TaskMessageData.TYPE_TIMING.equals(taskMessage.getType())) {//定时一次
                Date date = DateUtil.parseToDate(timeStr, "yyyy-MM-dd HH:mm:ss");
                Long time = (date.getTime() - new Date().getTime()) / 1000;
                if (time > 0) {
                    RedisUtil.set(String.valueOf(taskMessage.getId()),taskMessage.getType(),time);
                }
            } else if (TaskMessageData.TYPE_LOOP.equals(taskMessage.getType())) {//循环定时
                if (TaskMessageData.DAY.equals(taskMessage.getTimeType())) {//每天
                    Long dayTime = TaskMessageData.getDayTime(timeStr);
                    RedisUtil.set(String.valueOf(taskMessage.getId()),taskMessage.getType(),dayTime);
                } else if (TaskMessageData.WEEK.equals(taskMessage.getTimeType())) {//每周
                    Long weekTime = TaskMessageData.getWeekTime(timeStr);
                    RedisUtil.set(String.valueOf(taskMessage.getId()),taskMessage.getTitle(),weekTime);
                } else if (TaskMessageData.MONTH.equals(taskMessage.getTimeType())) {//每月
                    Long monthTime = TaskMessageData.getMonthTime(timeStr);
                    RedisUtil.set(String.valueOf(taskMessage.getId()),taskMessage.getType(),monthTime);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}
